home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / FLEX-TC_ / CCL.C < prev    next >
Text File  |  1990-01-02  |  4KB  |  180 lines

  1. /* ccl - routines for character classes */
  2.  
  3. /*
  4.  * Copyright (c) 1989 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant to
  11.  * contract no. DE-AC03-76SF00098 between the United States Department of
  12.  * Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted
  15.  * provided that the above copyright notice and this paragraph are
  16.  * duplicated in all such forms and that any documentation,
  17.  * advertising materials, and other materials related to such
  18.  * distribution and use acknowledge that the software was developed
  19.  * by the University of California, Berkeley.  The name of the
  20.  * University may not be used to endorse or promote products derived
  21.  * from this software without specific prior written permission.
  22.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  23.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  25.  */
  26.  
  27. #ifndef lint
  28.  
  29. static char copyright[] =
  30.     "@(#) Copyright (c) 1989 The Regents of the University of California.\n";
  31. static char CR_continuation[] = "@(#) All rights reserved.\n";
  32.  
  33. static char rcsid[] =
  34.     "@(#) $Header: ccl.c,v 2.0 89/06/20 15:49:07 vern Locked $ (LBL)";
  35.  
  36. #endif
  37.  
  38. #include "flexdef.h"
  39.  
  40. /* ccladd - add a single character to a ccl
  41.  *
  42.  * synopsis
  43.  *    int cclp;
  44.  *    char ch;
  45.  *    ccladd( cclp, ch );
  46.  */
  47.  
  48. ccladd( cclp, ch )
  49. int cclp;
  50. char ch;
  51.  
  52.     {
  53.     int ind, len, newpos, i;
  54.  
  55.     len = ccllen[cclp];
  56.     ind = cclmap[cclp];
  57.  
  58.     /* check to see if the character is already in the ccl */
  59.  
  60.     for ( i = 0; i < len; ++i )
  61.     if ( ccltbl[ind + i] == ch )
  62.         return;
  63.  
  64.     newpos = ind + len;
  65.  
  66.     if ( newpos >= current_max_ccl_tbl_size )
  67.     {
  68.     current_max_ccl_tbl_size += MAX_CCL_TBL_SIZE_INCREMENT;
  69.  
  70.     ++num_reallocs;
  71.  
  72.     ccltbl = reallocate_character_array( ccltbl, current_max_ccl_tbl_size );
  73.     }
  74.  
  75.     ccllen[cclp] = len + 1;
  76.     ccltbl[newpos] = ch;
  77.     }
  78.  
  79.  
  80. /* cclinit - make an empty ccl
  81.  *
  82.  * synopsis
  83.  *    int cclinit();
  84.  *    new_ccl = cclinit();
  85.  */
  86.  
  87. int cclinit()
  88.  
  89.     {
  90.     if ( ++lastccl >= current_maxccls )
  91.     {
  92.     current_maxccls += MAX_CCLS_INCREMENT;
  93.  
  94.     ++num_reallocs;
  95.  
  96.     cclmap = reallocate_integer_array( cclmap, current_maxccls );
  97.     ccllen = reallocate_integer_array( ccllen, current_maxccls );
  98.     cclng = reallocate_integer_array( cclng, current_maxccls );
  99.     }
  100.  
  101.     if ( lastccl == 1 )
  102.     /* we're making the first ccl */
  103.     cclmap[lastccl] = 0;
  104.  
  105.     else
  106.     /* the new pointer is just past the end of the last ccl.  Since
  107.      * the cclmap points to the \first/ character of a ccl, adding the
  108.      * length of the ccl to the cclmap pointer will produce a cursor
  109.      * to the first free space
  110.      */
  111.     cclmap[lastccl] = cclmap[lastccl - 1] + ccllen[lastccl - 1];
  112.  
  113.     ccllen[lastccl] = 0;
  114.     cclng[lastccl] = 0;    /* ccl's start out life un-negated */
  115.  
  116.     return ( lastccl );
  117.     }
  118.  
  119.  
  120. /* cclnegate - negate a ccl
  121.  *
  122.  * synopsis
  123.  *    int cclp;
  124.  *    cclnegate( ccl );
  125.  */
  126.  
  127. cclnegate( cclp )
  128. int cclp;
  129.  
  130.     {
  131.     cclng[cclp] = 1;
  132.     }
  133.  
  134.  
  135. /* list_character_set - list the members of a set of characters in CCL form
  136.  *
  137.  * synopsis
  138.  *     int cset[CSIZE + 1];
  139.  *     FILE *file;
  140.  *     list_character_set( cset );
  141.  *
  142.  * writes to the given file a character-class representation of those
  143.  * characters present in the given set.  A character is present if it
  144.  * has a non-zero value in the set array.
  145.  */
  146.  
  147. list_character_set( file, cset )
  148. FILE *file;
  149. int cset[];
  150.  
  151.     {
  152.     register int i;
  153.     char *readable_form();
  154.  
  155.     putc( '[', file );
  156.  
  157.     for ( i = 1; i <= CSIZE; ++i )
  158.     {
  159.     if ( cset[i] )
  160.         {
  161.         register int start_char = i;
  162.  
  163.         putc( ' ', file );
  164.  
  165.         fputs( readable_form( i ), file );
  166.  
  167.         while ( ++i <= CSIZE && cset[i] )
  168.         ;
  169.  
  170.         if ( i - 1 > start_char )
  171.         /* this was a run */
  172.         fprintf( file, "-%s", readable_form( i - 1 ) );
  173.  
  174.         putc( ' ', file );
  175.         }
  176.     }
  177.  
  178.     putc( ']', file );
  179.     }
  180.